package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.result.Result;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 角色相关接口
 * @Author: Ian
 * @Date: 2023/9/25 14:47
 */
@RestController
@Api(tags = "角色接口")
@RequestMapping("/admin/acl/role")
@Slf4j
@CrossOrigin // 解决跨域问题
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色分页查询
     * @param pageNum 当前页
     * @param pageSize 每页记录数
     * @param roleQueryVo 条件查询封装对象
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    @ApiOperation("角色分页查询")
    public Result queryPageRole(@PathVariable("pageNum")Long pageNum,
                                @PathVariable("pageSize")Long pageSize,
                                RoleQueryVo roleQueryVo
                                ){
        log.info("角色分页查询数据 pageNum:{}, pageSize:{},查询的数据:{}",pageNum,pageSize,roleQueryVo);

        // 创建分页对象,传入当前页,每页记录数
        Page<Role> page = new Page<>(pageNum,pageSize);

        // 调用service方法实现条件分页查询,返回分页对象
        IPage iPage = roleService.queryPageRole(page,roleQueryVo);

        return Result.ok(iPage);
    }

    /**
     * 根据id查询角色
     * @param id 角色Id
     * @return
     */
    @GetMapping("/get/{id}")
    @ApiOperation("根据Id查询角色")
    public Result queryRoleById(@PathVariable("id") Long id){

        Role byId = roleService.getById(id);

        return Result.ok(byId);
    }

    /**
     * 新建角色
     * @param role
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新建角色")
    public Result saveRole(@RequestBody Role role){
        boolean save = roleService.save(role);

        if (save == false){
            return Result.fail(null);
        }else{
            return Result.ok(null);
        }
    }

    /**
     * 根据ID删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("根据ID删除角色")
    public Result removeRoleById(@PathVariable("id") Long id){

        boolean removeById = roleService.removeById(id);

        if (removeById == false){
            return Result.fail(null);
        }else{
            return Result.ok(null);
        }
    }

    /**
     * 批量删除角色
     * @param ids
     * @return
     */
    @DeleteMapping("/batchRemove")
    @ApiOperation("批量删除多个角色")
    public Result batchRemove(@RequestBody List<Long> ids){
        log.info("批量删除角色ID:" + ids);
        boolean b = roleService.removeByIds(ids);

        if (b == false){
            return Result.fail(null);
        }else{
            return Result.ok(null);
        }
    }


    /**
     * 修改角色信息
     * @param role
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改角色信息")
    public Result updateRole(@RequestBody Role role){
        log.info("修改角色信息: " + role);
        boolean b = roleService.updateById(role);
        if (b == false){
            return Result.fail(null);
        }else{
            return Result.ok(null);
        }
    }
}
